package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.logic.MenuListManager;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.StyleSheet;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	
	public class NotepadLogic
	{
		public var ui:NotepadUI ; 
		public static var _self:NotepadLogic ; 
		private var _model:ModelLocator; 
		private var pageIndex:uint; 
		private var _indexOneCount:int = -1 ; //可以获得铜币总数
		private var _bingManage:BindManage;
		private var money:int ; 
		private var moneyTop:int ;
		private var renown:int ; 
		private var renownTop:int ;  
		private var list:Array ; //
		private  var date:Date ; 
		private var  isOK:int ; 
		private var 	chatArr:Array;//闲聊
		private var 	inforArr:Array; //杂事数组
		
		
		private var nowGold:int=0 ; 
		private var maxGold:int=0 ; 
		private var nowRenown:int=0 ; 
		private var maxRenown:int=0 ; 
		
		
		public var isHaveMsg:Boolean =true ; 
		private var _isOk:Boolean  =false ; 
		
		private var strArray:Array  ;
		public static function getInstence():NotepadLogic
		{
			if(_self==null)
			{
				_self=new NotepadLogic(); 
			}
			return _self;
		}
		public function NotepadLogic()
		{
			
		}
		//初始化数据
		public function Init():void
		{
			strArray= ["Lobby_11","Lobby_12","Lobby_13","Lobby_14","Lobby_15","Lobby_16",
						"Lobby_17","Lobby_18","Lobby_19","Lobby_20","Lobby_21","Lobby_22",
						"Lobby_23","Lobby_24","Lobby_25","Lobby_26","Lobby_27","Lobby_28",
						"Lobby_29","Lobby_30","Lobby_31","Lobby_32","Lobby_33","Lobby_34"] ; 
			inforArr= new Array(); 
			chatArr= new Array();
			list= new Array(); 
			date= new Date();
			ui			=NotepadUI.getInstence() ;
			_model      = ModelLocator.getInstance();
			ui.nxtap.addItem("江湖历练");//------收益
			ui.nxtap.addItem("江湖闲聊");//------聊天
			ui.nxtap.addItem("江湖杂事");//------消息
			_bingManage = BindManage.getInstance();
			var txtStyle:StyleSheet	=	new StyleSheet();
			pageIndex = 0 ; 
			ui.nxtext.multiline =true; 
			ui.nxtext.wordWrap  = true ; 
			/*
			txtStyle.setStyle("a:link"		,{color:"#"+StaticIni.getIniVar("Css_a_Task","link"),textDecoration:"underline"});
			txtStyle.setStyle("a:visited"	,{color:"#"+StaticIni.getIniVar("Css_a_Task","visited")});
			txtStyle.setStyle("a:hover"	, { color:"#" + StaticIni.getIniVar("Css_a_Task", "hover"), textDecoration:"none" });
			ui.nxtext.styleSheet	=	txtStyle;*/
			
			//接受历练
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KNIGHT_NOTES),showInfo)
			//打招呼
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KNIGHT_CALL),notepadCall);
			//当界面时单个消息
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KNIGHT_UPDATE_NOTE),oneInfo);
			//领取成功和失败
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KNIGHT_GET_PRICE_SUCCESS),getGoldAndRenown);
			//接受离线杂事
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KNIGHT_EXIST_OFFINE_CHROS),getoffLineMix);
			//接受离线打招呼
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KNIGHT_EXIST_OFFINE_GREETING),getoffLineHello);
			//江湖杂事
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_KNIGHT_CHORES),getChores);
			
//			NotepadLogic.getInstence().ui.created==true ; 
			
		}
		public function BeforeShow():void
		{
			ui.nxtext.htmlText ="" ;
			pageIndex =0 ; 
			updateNews(list); 
			bind(); 
			//请求历练数据
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_REQUEST_NOTES);
			//请求离线杂事
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_OFFINE_CHROS);
			//请求离线打招呼
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_OFFINE_GREETING);
			
			addListener() ; 
			ui.nxtap.selected =0;
			ui.label_1.htmlText=StaticIni.getSwfLangStrVar("UIString_1000150",[nowGold,maxGold]);
			ui.label_2.htmlText=StaticIni.getSwfLangStrVar("UIString_1000151",[nowRenown,maxRenown]);
		}
		public function AfterHide():void
		{
			pageIndex = 0 ; 
			ui.nxtext.htmlText ="" ; 
			//ui.label_1.htmlText = "";
			//ui.label_2.htmlText= "";
			//ui.label_3.htmlText = "" ;
			//ui.label_4.htmlText ="";
			removeListener()  ; 
			unbind();
			//nowGold=0;
			//maxGold=0;
			//nowRenown=0;
			//maxRenown=0;
			
		}
		//按钮监听
		private function addListener():void 
		{
			ui.nxtap.addEventListener(MouseEvent.CLICK,pageHandler); 
			ui.pullDown.addEventListener(MouseEvent.CLICK,pullDownHandler); 
			ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,closeHandler);
			//文本添加 监听 你所连接的 字段
			ui.nxtext.addLinkClickHandel(linkHandle);
		}
		
		private function linkHandle(val:String):void
		{
			//			//悬浮按钮
			if(pageIndex==0)
			{	
				MenuListManager.clickNotepadName(val);
			}
			else if (pageIndex==1)
			{
				MenuListManager.clickNotepadCall(val);
			}
			
		}
		//移除监听
		private function removeListener():void
		{
			ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,closeHandler);
			ui.pullDown.removeEventListener(MouseEvent.CLICK,pullDownHandler); 
		}
		//关闭窗口
		private function closeHandler(e:NxWindowEvent):void
		{
			ui.showOrHide();
		}
		//收益上限提高
		//		private function rewarHandler(e:MouseEvent):void
		//		{
		////			
		//			var list:Array = [1,1,1,1,1,1,1]; 
		//			chatArr.push(list);
		//			if(pageIndex==1){
		//				ui.nxtext.htmlText+=updateChatNews(list);
		//			}else if (pageIndex==0)
		//			{
		//				ui.nxtext.htmlText+=getStr(list);
		//			}
		//			ui.nxtext.setScroll(1);
		//			//SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_JH_SEACH_ADD_CONSUME); 
		////			trace("收益")
		//			
		//		}
		//领取
		private function pullDownHandler(e:MouseEvent):void
		{
			//20261
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_KNIGHT_GET_PRICE);
//			trace("领取")
		}
		//切换页签
		private function pageHandler(e:MouseEvent):void
		{
			
			if(pageIndex==ui.nxtap.selected)return ; 
			pageIndex=ui.nxtap.selected ; 
			switch(pageIndex)
			{
				case 0:
					ui.nxtext.htmlText ="" ;
					updateNews(list);
//					trace(pageIndex);
					break ; 
				case 1:
					ui.nxtext.htmlText ="" ; 
					updateChat(chatArr);
//					trace(pageIndex);
					break ; 
				case 2:
					ui.nxtext.htmlText ="" ; 
					updateinfor(inforArr);
//					trace(pageIndex);
					break ; 
				
			}
			
		}
		private function getNotepadInfor():void
		{
			//inforArr.push(e.plist); 
			
		}
		//接受所有的数据 打开界面的时候
		private function showInfo(e:CustomMsgEvent):void 
		{
			
			//			if(ui.created== true )
			//			{
			var list1 :Array = e.pList ; 
			if(e.pList.length==0)return ; 
			
			//				for(var i:uint =0;  i<list1[0]; i++)
			//				{
			//					var totalArr:Array= [list1[(i*6)+1],list1[(i*6)+2],list1[(i*6)+3],list1[(i*6)+4],list1[(i*6)+5],list1[(i*6)+6]]; 
			//					list.push(totalArr);
			//				}
			//				if(ui.created== true )
			//				{
			//					updateNews(list);
			//				}
			
			if(list1[0]==0){
				nowGold=list1[uint(num+1)]; 
				maxGold=list1[uint(num+2)];
				nowRenown=list1[uint(num+3)];	
				maxRenown=list1[uint(num+4)];
			}else if(list1[0]>=1)
			{
				for(var i:uint =0;  i<list1[0]; i++)
				{
					var totalArr:Array= [list1[(i*6)+1],list1[(i*6)+2],list1[(i*6)+3],list1[(i*6)+4],list1[(i*6)+5],list1[(i*6)+6]]; 
					list.push(totalArr);
				}
				if(ui.created== true)
				{
					updateNews(list);
				}
				
				var num:int = int((list1[0])*6) ; 
				nowGold=list1[uint(num+1)]; 
				maxGold=list1[uint(num+2)];
				nowRenown=list1[uint(num+3)];
				maxRenown=list1[uint(num+4)];
			}
			
			//				ui.label_1.htmlText=list1[uint(num+1)].toString();
			//				
			//				ui.label_2.htmlText= list1[uint(num+3)].toString();
			//				
			//				ui.label_3.htmlText=maxGold.toString();
			//				
			//				ui.label_4.htmlText=maxRenown.toString();
			ui.label_1.htmlText=StaticIni.getSwfLangStrVar("UIString_1000150",[nowGold,maxGold]);
			ui.label_2.htmlText=StaticIni.getSwfLangStrVar("UIString_1000151",[nowRenown,maxRenown]);
			//			}
			
			
		}
		//接受单条消息 铜币和声望消息---------------------------------
		private function oneInfo(e:CustomMsgEvent):void
		{
			this.list.push(e.pList)
			
			if(ui.created== true)
			{
//				BottomIndexLogic.getInstance().getStopGlow();
				if(pageIndex==0)
				{
					ui.nxtext.htmlText+=getStr(e.pList); 
				}
				
			}else if(ui.created== false){
//				BottomIndexLogic.getInstance().getStartGlow();
			}
			
			if(e.pList[4]==0)
			{
				nowGold+=int(e.pList[5]);
				ui.label_1.htmlText=nowGold.toString();
				
			}else if (e.pList[4]==10) {
				
				nowRenown+=int(e.pList[5]);
				ui.label_2.htmlText=nowRenown.toString();
			}
		}
		//把数据 换成消息
		private function updateNews(list:Array):void
		{
			if(list.length==0||list[0]=="")return ; 
			var str:String=""; 
			for(var i:uint = 0; i <list.length;i++)
			{
				str+=getStr(list[i])+"<br>"+"<br>";
				
				//ui.nxtext.setScroll(1);
			}
			ui.nxtext.htmlText=str;
			
			
		}
		//防止出现 NxText 空白 行
		private function getStr(list:Array):String 
		{
			if(list.length ==0)return "" ;
			ui.nxtext.setScroll(1);
			date.time=Number(list[1])*1000;
			var str:String ="" ;
			switch(int(list[2]))
			{
				case 0:
					str = StaticIni.getSwfLangStrVar("UIString_1000142",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,list[3],list[3],list[5],list[4]]);
					return str;
					break ;
				case 1:
					str =StaticIni.getSwfLangStrVar("UIString_1000143",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,list[3],list[3],list[5],list[4]]);
					return str;
					break ; 
				
			}
			
			return "";
		}
		//闲聊*********************************
		//时间 类型 名字 主动还是被动
		private function notepadCall(e:CustomMsgEvent):void 
		{
			
			if(e.pList.length==0||e.pList[0]=="")return ; 
			
			var arrOneNews:Array=[];
			if(e.pList[0]==1)
			{
				arrOneNews = e.pList.splice(1);
				this.chatArr.push(arrOneNews);
				
			}else if (e.pList[0]>=2)
			{
				for(var i:uint= 0; i<e.pList[0] ;i++)
				{
					var totalArr:Array= [e.pList[(i*4)+1],e.pList[(i*4)+2],e.pList[(i*4)+3],e.pList[(i*4)+4]];
					arrOneNews.push(totalArr);
					chatArr.push(totalArr);
				}
			}
			if(ui.created== true)	
			{
//				BottomIndexLogic.getInstance().getStopGlow();
				if(pageIndex==1)
				{
					if(arrOneNews.length==1)
					{
						ui.nxtext.htmlText+=updateChatNews(arrOneNews);
					}else
					{
						updateChat(chatArr);
					}
					
				}else if (_isOk==true)
				{
					turnPageIndex();
					_isOk= false ; 
				}
				
				
			}else 
			{
//				BottomIndexLogic.getInstance().getStartGlow();
				
			}
			
			
		}
		//绑定领取按钮------------------------------------------------------------------------------
		private function bind():void 
		{
			if(_indexOneCount==-1)
			{
				_indexOneCount = _bingManage.BindProp(_model.scene.Role,"KNIGHTGetPriceNUM",this,"getPullDwon");
			}
		}
		//解除绑定
		private function unbind():void
		{
			if(_indexOneCount!=-1)
			{
				_indexOneCount=_bingManage.UnbindProp(_model.scene.Role,"KNIGHTGetPriceNUM",_indexOneCount);
			}
		}
		//
		public function set getPullDwon(value:int):void
		{
			isOK= value ; 
			if(isOK==0)
			{
				ui.pullDown.enabled = true  ; 
			}else if (isOK==1)
			{
				ui.pullDown.enabled =false ; 
			}
			
		}
		//************************************领取 按钮接受消息处理方案*******************************
		private function getGoldAndRenown(e:CustomMsgEvent):void 
		{
			var list:Array =e.pList ; 
			nowGold=list[0];
			nowRenown=list[1];
			//			ui.label_1.htmlText=list[0].toString();
			//			ui.label_2.htmlText= list[1].toString();
			
			ui.label_1.htmlText=StaticIni.getSwfLangStrVar("UIString_1000150",[nowGold,maxGold]);
			ui.label_2.htmlText=StaticIni.getSwfLangStrVar("UIString_1000151",[nowRenown,maxRenown]);
			
		}
		//
		private function updateChat(arr:Array):void
		{
			if(arr.length==0||arr[0]=="")return ; 
			var str:String=""; 
			for(var i:uint = 0; i <arr.length ;i++)
			{
				str+=updateChatNews(arr[i])+"<br>"+"<br>";
			}
			ui.nxtext.htmlText=str;
			
		}
		//江湖闲聊
		private function updateChatNews(arr:Array):String
		{
			var str:String =""; 
			ui.nxtext.setScroll(1);
			date.time = Number(arr[0])*1000;
			switch(int(arr[3]))
			{
				case 0:
					switch(arr[2])
					{
						case 1:
							str =StaticIni.getSwfLangStrVar("PlayerFace_009",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						case 2:
							str =StaticIni.getSwfLangStrVar("PlayerFace_010",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						case 3:
							str =StaticIni.getSwfLangStrVar("PlayerFace_011",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						case 4 :
							str =StaticIni.getSwfLangStrVar("PlayerFace_012",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ;
						case 5:
							str =StaticIni.getSwfLangStrVar("UIString_1000145",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						
					}
					break; 
				case 1:
					switch(arr[2])
					{
						case 1:
							str =StaticIni.getSwfLangStrVar("PlayerFace_005",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						case 2:
							str =StaticIni.getSwfLangStrVar("PlayerFace_006",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						case 3:
							str =StaticIni.getSwfLangStrVar("PlayerFace_007",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						case 4 :
							str =StaticIni.getSwfLangStrVar("PlayerFace_008",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ;
						case 5:
							str =StaticIni.getSwfLangStrVar("UIString_1000146",[date.fullYear,date.month+1,date.date,date.hours,date.minutes,arr[1],arr[1]]);
							return str;
							break ; 
						
					}
					break ; 
			}
			
			
			return ""; 
		}
		//离线接受杂事
		private function getoffLineMix(e:CustomMsgEvent):void
		{
			var arrMix:Array=e.pList;  
			
			if(ui.created==false)
			{
//				BottomIndexLogic.getInstance().getStartGlow();
			}
			
			
		}
		//离线接受打招呼
		private function getoffLineHello(e:CustomMsgEvent):void
		{
			var arrHello:Array=e.pList; 
			
			var str:String="" ; 
			if(ui.created==false)
			{
//				BottomIndexLogic.getInstance().getStartGlow();
			}
		}
		//接受杂事消息
		private function getChores(e:CustomMsgEvent):void 
		{
			if(e.pList.length==0||e.pList[0]=="")return ; 
			
			var arrOneNews:Array=[];
			
			if(e.pList[0]==1)
			{
				arrOneNews = e.pList.splice(1);
				this.inforArr.push(arrOneNews);
			}
			else if (e.pList[0]>=2)
			{
				for(var i:uint= 0; i<e.pList[0] ;i++)
				{
					var totalArr:Array= [e.pList[(i*3)+1],e.pList[(i*3)+2],e.pList[(i*3)+3]];
					arrOneNews.push(totalArr);
					inforArr.push(totalArr);
				}
			}
			if(ui.created== true)	
			{
//				BottomIndexLogic.getInstance().getStopGlow();
				if(pageIndex==2)
				{
					if(arrOneNews.length==1)
					{
						ui.nxtext.htmlText+=updateChatNews(arrOneNews);
					}else
					{
						updateinfor(inforArr);
					}
				}
				
			}else 
			{
//				BottomIndexLogic.getInstance().getStartGlow();
				
			}
		}//----------------------------------------------------
		private function updateinfor(arr:Array):void 
		{
			if(arr.length==0||arr[0]=="")return ; 
			var str:String=""; 
			for(var i:uint = 0; i <arr.length ;i++)
			{
				str+=updateinforNews(arr[i])+"<br>"+"<br>";
			}
			ui.nxtext.htmlText=str;
		}
		private function turnPageIndex():void
		{
			ui.nxtap.selected =1;
			pageIndex=ui.nxtap.selected ; 
			ui.nxtext.htmlText ="" ; 
			updateChat(chatArr);
			
		}
		//
		private function updateinforNews(arr:Array):String
		{
			if(arr.length==0||arr[0]=="")return "" ;  
			ui.nxtext.setScroll(1);
			date.time = Number(arr[0])*1000;
			
			var count:int=arr[1];
			
			//trace("NotepadLogic.updateinforNews(arr)",arr[1],date,(new Date).time);
			var array:Array = analysisStr(String(arr[2]))
			var num:int = array.length; 
			var str:String = "" ; 
			
			count+=int(array.shift());
			if(count==22)
			{
				array[0]=StaticIni.getSwfLangStr(array[0].toString());
			}else if(count==23)
			{
				array[1]=StaticIni.getSwfLangStr(array[1].toString());
			}
			
			array.unshift(date.fullYear,date.month+1,date.date,date.hours,date.minutes);
			
			str =StaticIni.getSwfLangStrVar(strArray[int(count)],array);
			trace("NotepadLogic.updateinforNews(arr)",array,"str=",str);
			
			return str;
			
			
		}//------
		public function set isOk(ok:Boolean):void
		{
			_isOk	=	ok;
		}
		private function analysisStr(str:String):Array
		{
			if(str=="")return null ;
			var bufferArray:Array	=	str.split(";");
			return bufferArray ; 
		}
		
	}
}